package com.xb.Util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class JDBCUtils {
    public static Connection getConnection() {
        Connection connection = null;
        try {
            InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mysqlConnectInfo.properties");
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            String driver = properties.getProperty("driver");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            //通过反射加载驱动
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
    private static  DataSource dataSource;
    static {
        try {
            InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection02() throws SQLException {
        return dataSource.getConnection();
    }

    public static void closeConnection(Connection connection) {
        try {
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeConAndPs(Connection connection, PreparedStatement preparedStatement) {
        try {
            if (connection != null) {
                connection.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeConAndPsAndRs(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        try {
            if (connection != null) {
                connection.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (resultSet != null)
                resultSet.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void closeConAndPsAndFile(Connection connection, PreparedStatement preparedStatement, FileInputStream fileInputStream) {
        try {
            if (connection != null) {
                connection.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //创建一个通用的增删改的sql语句
    public static void DclSql(String sql, List<Object> param) {
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            connection = getConnection();
            ps = connection.prepareStatement(sql);
            int index = 1;
            for (Object o : param) {
                ps.setObject(index, o);
                index++;
            }
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConAndPs(connection, ps);
        }
    }

    //创建一个通用的增删改的sql语句
    public static void DclSql02(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            connection = getConnection();
            ps = connection.prepareStatement(sql);
            int len = args.length;
            for (int i = 1; i <= len; i++) {
                ps.setObject(i, args[i - 1]);
            }
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConAndPs(connection, ps);
        }
    }

    //创建一个通用的增删改的sql语句
    public static void DmlSqlByTransaction(Connection connection, String sql, Object... args) {
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            int len = args.length;
            for (int i = 1; i <= len; i++) {
                ps.setObject(i, args[i - 1]);
            }
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConAndPs(null, ps);
        }
    }

    //查询 多行
    public static <T> List<T> selectGetData(String sql, Class<T> cls, Object... args) {
        T t = null;
        Connection connection = null;
        PreparedStatement ps = null;
        List<T> list = new ArrayList<>();
        try {
            connection = getConnection();
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            ResultSet resultSet = ps.executeQuery();
            //获取结果集的元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            //获取总列数
            int columnCount = metaData.getColumnCount();
            while (resultSet.next()) {
                t = cls.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //getCatalogName列名
                    /// 1. 获取列的别名
                    String catalogName = metaData.getColumnLabel(i + 1);
                    // 2. 根据列名获取对应数据表中的数据
                    Object object = resultSet.getObject(catalogName);
                    // 3. 将数据表中得到的数据，封装进对象
                    Field declaredField = cls.getDeclaredField(catalogName);
                    declaredField.setAccessible(true);
                    declaredField.set(t, object);
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConAndPs(connection, ps);
        }
        return null;
    }

    //查询 单行
    public static <T> T selectGetData02(String sql, Class<T> cls, Object... args) {
        T t = null;
        Connection connection = null;
        PreparedStatement ps = null;

        try {
            connection = getConnection();
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            ResultSet resultSet = ps.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            //获取的他的总列数
            int columnCount = metaData.getColumnCount();
            while (resultSet.next()) {
                //创建对象
                t = cls.getConstructor().newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //获取行的值
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    Object object = resultSet.getObject(columnLabel);
                    Field field = cls.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t, object);
                }
            }
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConAndPs(connection, ps);
        }
        return null;
    }

    //这个查询是关于事务的
    public static <T> List<T> selectGetDataBytrasaction(Connection connection, String sql, Class<T> cls, Object... args) {
        T t = null;
        PreparedStatement ps = null;
        List<T> list = new ArrayList<>();
        try {
            ps = connection.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            ResultSet resultSet = ps.executeQuery();
            //获取结果集的元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            //获取总列数
            int columnCount = metaData.getColumnCount();
            while (resultSet.next()) {
                t = cls.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //getCatalogName列名
                    /// 1. 获取列的别名
                    String catalogName = metaData.getColumnLabel(i + 1);
                    // 2. 根据列名获取对应数据表中的数据
                    Object object = resultSet.getObject(catalogName);
                    // 3. 将数据表中得到的数据，封装进对象
                    Field declaredField = cls.getDeclaredField(catalogName);
                    declaredField.setAccessible(true);
                    declaredField.set(t, object);
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConAndPs(connection, ps);
        }
        return null;
    }
}
